﻿Imports System.Windows
Imports OMANE.Common.UI

Public Class ViewQueryGenerator


    Sub New(ByVal schema As DataTable)

        InitializeComponent()




    End Sub

    Sub New()

        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().

    End Sub

    Private _source As VMDataModel
    Sub New(ByVal con As OleDb.OleDbConnection, ByVal workspace As String, ByVal source As VMDataModel)
        ' TODO: Complete member initialization 
        InitializeComponent()

        Dim s = 1
        Dim qtbls = From elt In con.GetSchema("Tables").DefaultView()
                    Where elt("TABLE_NAME").ToString().StartsWith("ICHOU")
                    Select CStr(elt("TABLE_NAME")) Take 1

        Dim qcols = From elt In con.GetSchema("Columns").DefaultView()
                    Where qtbls.FirstOrDefault() = CStr(elt("TABLE_NAME"))
                    Select elt

        DataContext = New With {.Columns = qcols.ToArray()}


        'Dim lst As New List(Of Object)

        'Dim sch = con.GetSchema("Columns").DefaultView()

        'For Each e In sch
        '    lst.Add(e("TABLE_NAME") & "." & e("COLUMN_NAME") & " AS " & e("DATA_TYPE"))
        'Next

        'Dim o = 1
        _source = source
        ModuleIndicateurs.AppInstance.GetService(Of Common.UI.IUIService)().ShowMessage(Me)
    End Sub

    Private Sub ColumnsMouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim rv = CType(sender.DataContext, DataRowView)
        DragDrop.DoDragDrop(sender, rv, DragDropEffects.Copy)
    End Sub

    Private selectedcols As New List(Of Object)
    Private Sub ListBox1_Drop(ByVal sender As System.Object, ByVal e As System.Windows.DragEventArgs)
        ' selectedcols.Add(e.Data.GetData(GetType(DataRowView)))
        '    ListBox1.ItemsSource = selectedcols

        'Dim ui = ModuleIndicateurs.AppInstance.GetService(Of IUIService)()
        'ui.ShowMessage(Me.Content.Resources("transSP"))
        Dim lstb = CType(sender, Controls.ListBox)
        lstb.Items.Add(e.Data.GetData(GetType(DataRowView)))
        If String.IsNullOrEmpty(lstb.DisplayMemberPath) Then
            lstb.DisplayMemberPath = "COLUMN_NAME"
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim q = From elt As DataRowView In ListBox1.Items Select CStr(elt("COLUMN_NAME"))
        Dim q2 = From elt As DataRowView In ListBox3.Items Select CStr(elt("COLUMN_NAME"))

        Dim qq = "SELECT " & String.Join(", ", q.ToArray()) & vbCrLf & _
            " FROM ICHOU.maintable" & vbCrLf & _
            " GROUP BY " & String.Join(", ", q2.ToArray())
        _source.QueryString = qq
        Window.GetWindow(Me).Close()
    End Sub
End Class
